#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2020/11/24 下午7:30
# @Author  : zkw
# @File    : PiePlotMaker.py
from math import pi

from bokeh.palettes import Category20c
from bokeh.plotting import Figure, figure
from bokeh.transform import cumsum

from plot.BaseMaker import BaseMaker
from plot.PlotInfo import PlotInfo

import pandas


class PiePlotMaker(BaseMaker):
    """
    调试 Pie 图用，请勿直接使用！
    """

    def make(self, info: PlotInfo, eventList: list) -> Figure:
        x = {
            'United States': 157,
            'United Kingdom': 93,
            'Japan': 89,
            'China': 63,
            'Germany': 44,
            'India': 42,
            'Italy': 40,
            'Australia': 35,
            'Brazil': 32,
            'France': 31,
            'Taiwan': 31,
            'Spain': 29
        }

        data = pandas.Series(x).reset_index(name='value').rename(columns={'index': 'country'})
        data['angle'] = data['value'] / data['value'].sum() * 2 * pi
        data['color'] = Category20c[len(x)]
        p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
                   tools="hover", tooltips="@country: @value", x_range=(-0.5, 1.0))

        p.wedge(x=0, y=1, radius=0.4,
                start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
                line_color="white", fill_color='color', legend_field='country', source=data)

        p.axis.axis_label = None
        p.axis.visible = False
        p.grid.grid_line_color = None
        return p
